var path = require('path'),
    webpack = require('webpack'),
    config = require('./config'),
    srcDir = path.resolve(process.cwd(), 'client'),
    imagePath = path.resolve(srcDir, 'img'),
    cssPath = path.resolve(srcDir, 'css'),
    jsPath = path.resolve(srcDir, 'js'),
    viewsPath = path.resolve(srcDir, 'views'),
    nodeModulesPath = path.resolve(__dirname, 'node_modules'),
    autoprefixer = require('autoprefixer'),
    ExtractTextPlugin = require("extract-text-webpack-plugin"),
    publicPath = config.domain + '/',
    HtmlWebpackPlugin = require('html-webpack-plugin'),
    CopyWebpackPlugin = require('copy-webpack-plugin'),
    CleanPlugin = require('clean-webpack-plugin');

let webpackConfig = {
    cache: true,
    entry: {
         index: ['babel-polyfill', path.join(__dirname, "client/views/index.jsx")]
    },
    output: {
        publicPath: '/',
        path: path.join(__dirname, "build/static/"), //文件输出目录
        filename: "core/[name].[hash:5].js", //根据入口文件输出的对应多个文件名
        chunkFilename: "core/[name].[chunkhash:5].js"
    },
    resolve: {
        extensions: ['', '.js', '.jsx', '.json'], // 现在你require文件的时候可以直接使用require('file')，不用使用require('file.jsx')
        alias: { //配置别名，在项目中可缩减引用路径
            'img': imagePath,
            'css': cssPath,
            'js': jsPath,
            'views': viewsPath
        },
    },
    module: {
        rules: [{
            test: /\.(png|jpg|gif)$/,
            include: [imagePath],
            use: [{
                loader: 'style-loader',
                options: {
                    limit: 1000,
                    name: 'img/[name].[hash:5].[ext]'
                }
            }]
        }, {
            test: /\.css$/,
            use: ["style-loader", {
                loader: "css-loader",
                options: {
                    sourceMap: true
                }
            }, {
                loader: 'postcss-loader',
                options: {
                    plugins: [autoprefixer]
                }
            }]
        }, {
            test: /\.scss$/,
            use: ["style-loader", {
                loader: "css-loader",
                options: {
                    sourceMap: true
                }
            }, {
                loader: 'postcss-loader',
                options: {
                    plugins: [autoprefixer]
                }
            }, {
                loader: "sass-loader",
                options: {
                    sourceMap: true,
                    imagePath: imagePath,
                    includePaths: [cssPath]
                }
            }],
        }, {
            test: /\.jsx$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            use: [{
                loader: "babel-loader",
                options: {
                    presets: ['react', 'es2015']
                }
            }],
        }, {
            test: /\.js$/,
            exclude: [nodeModulesPath],
            include: [srcDir],
            use: [{
                loader: "babel-loader",
                options: {
                    presets: ['es2015']
                }
            }],
        }]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': '"production"'
        }),
        new webpack.DllReferencePlugin({ // 加快webpack打包速度
            context: __dirname,
            manifest: require('./build/static/dll/vendor-manifest.json')
        }),
        new CopyWebpackPlugin([{
            from: path.resolve(__dirname, './client/js/lib'),
            to: path.resolve(__dirname, './build/static/lib')
        }]),
        new ExtractTextPlugin('css/[name].[contenthash:5].css'),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: '"production"',
            },
        }),
        new webpack.optimize.UglifyJsPlugin({
            output: {
                comments: false,
            },
            compress: {
                warnings: false
            }
        }),
        new HtmlWebpackPlugin({
            filename: path.resolve(__dirname, './build/html/index.html'),
            template: path.resolve(__dirname, './build/html/index_dll.html'),
            inject: true
        })
    ]
};

if (process.env.clean) {
    webpackConfig.plugins.push(new CleanPlugin(['core'], {
        root: path.join(__dirname, "build/static"),
        verbose: true,
        dry: false
    }));
    webpackConfig.plugins.push(new CleanPlugin(['img'], {
        root: path.join(__dirname, "build/static"),
        verbose: true,
        dry: false
    }));
}

module.exports = webpackConfig;